package com.mycjj.android.activity;

import java.util.HashMap;
import java.util.Map;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SetTelActivity extends BaseActivity implements android.view.View.OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_tel);
		init();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("修改手机");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SetTelActivity.this.finish();
			}
		});
	}

	private EditText mEt_tel;
	private EditText mEt_code, mEt_old_tel;
	private Button mBtn_send;
	private boolean isRepeatSend = true;// 是否重新发送
	private Button mBtn_register;

	private void init() {
		initHeader();
		String mobile = Preferences.getSharedPreferences(mApp, "mobile", "");
		mEt_tel = (EditText) findViewById(R.id.et_tel);
		mEt_code = (EditText) findViewById(R.id.et_code);
		mEt_old_tel = (EditText) findViewById(R.id.et_old_tel);
		mEt_old_tel.setText(mobile);
		mEt_old_tel.setEnabled(false);
		mBtn_send = (Button) findViewById(R.id.btn_send);
		mBtn_send.setOnClickListener(this);
		mBtn_register = (Button) findViewById(R.id.btn_modify);
		mBtn_register.setOnClickListener(this);
	}

	private class ReduceTimeThread extends CountDownTimer {

		public ReduceTimeThread(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			isRepeatSend = false;
			mBtn_send.setText(millisUntilFinished / 1000 + "秒等待");
			mBtn_send.setEnabled(false);
		}

		@Override
		public void onFinish() {
			isRepeatSend = true;
			mBtn_send.setText("发送验证码");
			mBtn_send.setEnabled(true);
		}

	}

	@Override
	public void onClick(View v) {
		String phoneNum = mEt_tel.getText().toString();
		if (TextUtils.isEmpty(phoneNum)) {
			Toast.makeText(this, "手机号不能为空!", Toast.LENGTH_LONG).show();
			return;
		}
		switch (v.getId()) {
		case R.id.btn_send:
			sendMsgToTel(phoneNum);
			break;
		case R.id.btn_modify:
			String code = mEt_code.getText().toString().trim();
			String old_tel = mEt_old_tel.getText().toString().trim();
			if (TextUtils.isEmpty(old_tel)) {
				Toast.makeText(this, "旧手机号不能为空!", Toast.LENGTH_LONG).show();
				return;
			} else if (TextUtils.isEmpty(code)) {
				Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_LONG).show();
				return;
			}
			editMobile(phoneNum, code, old_tel);
			break;
		}
	}

	private void sendMsgToTel(final String tel) {
		Log.d("mark", "sendMsgToTel(String) in success! tel:" + tel);
		Map<String, String> map = new HashMap<String, String>();
		map.put("phone", tel);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("mobile", tel);
		params.put("type", "5");
		request = new ProjectHttpRequestImpl(this, true);
		request.requestPhoneMesWithDoneHandler(null, params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				BaseBean<?> bean = ProjectDataManage.getInstance().getBaseBean();
				if (bean != null) {
					if (bean.getCode() == 200) {
						mEt_code.setText((String) bean.getData());
						Log.d("mark", "repeatSend(String) in success! phoneNum:" + tel);
						if (isRepeatSend) {
							mHandler.sendEmptyMessage(0);
						}
					} else {
						Toast.makeText(SetTelActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(SetTelActivity.this, "bean is null!!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private void editMobile(String tel, String code, String oldmobile) {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("old_mobile", oldmobile);
		params.put("new_mobile", tel);
		params.put("code", code);
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestUserEditMobileWithDoneHandler("", params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(String content) {
				BaseBean<?> bean = ProjectDataManage.getInstance().getBaseBean();
				if (bean != null) {
					if (bean.getCode() == 200) {
						Toast.makeText(SetTelActivity.this, "修改成功!", Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(SetTelActivity.this,  bean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(SetTelActivity.this, "bean is null!!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private ReduceTimeThread reduceTimeTask;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Log.d("mark", "handleMessage what:" + msg.what);
			switch (msg.what) {
			case 0:
				reduceTimeTask = new ReduceTimeThread(60000, 1000);
				reduceTimeTask.start();
				break;
			}
		}
	};
}
